package cn.chenhao.basic.service.impl;

import cn.chenhao.basic.mapper.BasicMapper;
import cn.chenhao.basic.query.BasicQuery;
import cn.chenhao.basic.service.IBasicService;
import cn.chenhao.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BasicServiceImpl<T> implements IBasicService<T> {
    @Autowired
    private BasicMapper<T> basicMapper;

    /**
     * 添加功能
     */
    @Override
    @Transactional
    public void add(T t) {
        basicMapper.save(t);
    }

    /**
     * 修改
     */
    @Override
    @Transactional
    public void update(T t) {
        basicMapper.update(t);
    }

    /**
     * 删除单个
     */
    @Override
    @Transactional
    public void delete(Long id) {
        basicMapper.remove(id);
    }

    /**
     * 查询单个
     */
    @Override
    public T findById(Long id) {
        return basicMapper.loadById(id);
    }

    /**
     * 查询全部
     */
    @Override
    public List<T> FindAll() {
        return basicMapper.loadAll();
    }


    /**
     * 高级查询和分页
     */
    @Override
    public PageList<T> queryPage(BasicQuery query) {
        Integer total = basicMapper.queryCount(query);
        List<T> rows = basicMapper.queryData(query);
        return new PageList<T>(total,rows);
    }

    /**
     * 批量删除
     */
    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        basicMapper.patchDelete(ids);
    }

}
